﻿using System;
using System.Linq;
using System.Windows.Controls;
using CoreClasses;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tests.ServiceReferenceTest;

namespace Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void PublishGetHttp()
        {
            var service = new ServiceContractClient("BasicHttpBinding_IServiceContract");
            var ll = new TextBox() { Text = "test" };
            service.DeleteLayout("test");
            Assert.IsTrue(service.AddLayout(new Layout() { LayoutName = "test", LayoutElement = new SerializableFrameworkElement() { Element = new ContentControl() { Content = ll } }.GetContractData() }));
            var layouts = service.GetLayouts();
            var theLayout = layouts.FirstOrDefault(t => t.LayoutName == "test");
            Assert.IsNotNull(theLayout);
            Assert.IsTrue(theLayout.LayoutName == "test");
            Assert.IsNotNull(theLayout.LayoutElement);
            Assert.IsNotNull(theLayout.LayoutElement.Binary);
            var ld = new SerializableFrameworkElement.Data() {Binary = theLayout.LayoutElement.Binary};
            var serLayout = ld.GetObject();
            Assert.IsNotNull(serLayout.Element.Content);
            Assert.IsInstanceOfType(serLayout.Element.Content, typeof(TextBox));
            Assert.AreEqual((serLayout.Element.Content as TextBox)?.Text, "test");
            service.DeleteLayout("test");
            layouts = service.GetLayouts();
            theLayout = layouts.FirstOrDefault(t => t.LayoutName == "test");
            Assert.IsNull(theLayout);
        }
        [TestMethod]
        public void PublishGetHttps()
        {
            var service = new ServiceContractClient("BasicHttpsBinding_IServiceContract");
            var ll = new TextBox() { Text = "test" };
            service.DeleteLayout("test");
            Assert.IsTrue(service.AddLayout(new Layout() { LayoutName = "test", LayoutElement = new SerializableFrameworkElement() { Element = new ContentControl() { Content = ll } }.GetContractData() }));
            var layouts = service.GetLayouts();
            var theLayout = layouts.FirstOrDefault(t => t.LayoutName == "test");
            Assert.IsNotNull(theLayout);
            Assert.IsTrue(theLayout.LayoutName == "test");
            Assert.IsNotNull(theLayout.LayoutElement);
            Assert.IsNotNull(theLayout.LayoutElement.Binary);
            var ld = new SerializableFrameworkElement.Data() { Binary = theLayout.LayoutElement.Binary };
            var serLayout = ld.GetObject();
            Assert.IsNotNull(serLayout.Element.Content);
            Assert.IsInstanceOfType(serLayout.Element.Content, typeof(TextBox));
            Assert.AreEqual((serLayout.Element.Content as TextBox)?.Text, "test");
            service.DeleteLayout("test");
            layouts = service.GetLayouts();
            theLayout = layouts.FirstOrDefault(t => t.LayoutName == "test");
            Assert.IsNull(theLayout);
        }
       
        
    }
}
